package com.ridu.project.beshid.controller;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.api.Status;
import com.ridu.project.beshid.api.User;
import com.ridu.project.beshid.entity.UserEntity;
import com.ridu.project.beshid.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2017/2/13.
 */
@Controller
@RequestMapping(value = "user", produces = "application/json")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private UserService service;

    @RequestMapping(value = "redirectSys")
    public String redirectSys() {
        return "user";
    }

    @RequestMapping(value = "getAll")
    @ResponseBody
    public GeneralResponse<User> getAll(@RequestParam int currentPage, @RequestParam int pageSize) {
        return service.getAllUsers(currentPage, pageSize);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public GeneralResponse add(@RequestParam String username, @RequestParam String password, @RequestParam String category, @RequestParam String role) {
        return service.createUser(new UserEntity(username, password, category, role));
    }

    @RequestMapping(value = "modi", method = RequestMethod.POST)
    @ResponseBody
    public GeneralResponse modi(@RequestParam String uid, @RequestParam String password, @RequestParam String category, @RequestParam String role) {
        return service.changeUser(Long.parseLong(uid), password, category, role);

    }

    @RequestMapping(value = "updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public GeneralResponse updatePwd(String password) {
        GeneralResponse response = new GeneralResponse();
        try{
            UserEntity entity = getUser();
            if(entity == null){
                throw new Exception("请重新登录");
            }
            return service.changeUser(entity.getId(), password, null, entity.getRoles());
        } catch (Exception e){
            response.setStatus(Status.FAILED);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public GeneralResponse del(@RequestParam String ids) {
        GeneralResponse response = new GeneralResponse();
        if(StringUtils.isNotBlank(ids)){
            response = service.batchDelStatus(true, ids);
            if(response.getTotal() <= 0){
                response.setMessage("删除失败");
            }
        } else {
            response.setMessage("参数有误");
        }
        return response;
    }

    @RequestMapping(value = "redirectUserInfo")
    public String redirectUserInfo() {
        return "user_info";
    }

    @RequestMapping(value = "getUser", method = RequestMethod.GET)
    @ResponseBody
    public GeneralResponse getLoginUser() {
        GeneralResponse response = new GeneralResponse();
        try{
            UserEntity entity = getUser();
            if(entity == null){
                throw new Exception("请重新登录");
            }
            response.setData(entity);
            response.setStatus(Status.SUCCESS);
        } catch (Exception e){
            response.setMessage(e.getMessage());
            response.setStatus(Status.FAILED);
        }
        return response;
    }
}

